//package org.jfm.filesystems;

import java.util.Stack;
import java.io.ByteArrayInputStream;

import javax.xml.parsers.SAXParser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

public class YahooWeatherRSSHandler extends DefaultHandler {
	private static String ELEMENT_NAME_CHANNEL = "channel";
	private static String ELEMENT_NAME_TITLE = "title";
	private static String ELEMENT_NAME_LOCATION = "yweather:location";
	private static String ELEMENT_NAME_ATMOSPHERE = "yweather:atmosphere";
	private static String ELEMENT_NAME_ASTRONOMY = "yweather:astronomy";
	private static String ELEMENT_NAME_CONDITION = "yweather:condition";
	private static String ELEMENT_NAME_DESCRIPTION = "description";
	private static String ELEMENT_NAME_ITEM = "item";

	private static String ATTRIBUTE_NAME_CITY = "city";
	private static String ATTRIBUTE_NAME_REGION = "region";
	private static String ATTRIBUTE_NAME_COUNTRY = "country";
	private static String ATTRIBUTE_NAME_HUMIDITY = "humidity";
	private static String ATTRIBUTE_NAME_VISIBILITY = "visibility";
	private static String ATTRIBUTE_NAME_SUNRISE = "sunrise";
	private static String ATTRIBUTE_NAME_SUNSET = "sunset";
	private static String ATTRIBUTE_NAME_TEMPERATURE = "temp";

	private static String EMBEDED_ELEMENT_NAME_IMG = "img";
	private static String EMBEDED_ATTRIBUTE_NAME_SRC = "src";

	private static String ERROR_TITLE = "Yahoo! Weather - Error";

	private boolean embededImageMode;
	private String embedededImageSrc;
	private Stack<String> xmlStack;
	private WeatherData weather; 

	YahooWeatherRSSHandler(boolean embeded) {
		embededImageMode = embeded;
		weather = new WeatherData();
		xmlStack = new Stack<String>();
	}

	YahooWeatherRSSHandler() {
		this(false);
	}

	public WeatherData getWeatherData() {
		return weather;
	}

	public String getEmbededImageSrc() {
		return embedededImageSrc;
	}

	public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {

		if (embededImageMode && qName.equals(EMBEDED_ELEMENT_NAME_IMG)) {
			embedededImageSrc = attributes.getValue(EMBEDED_ATTRIBUTE_NAME_SRC);
		}
				
		if (qName.equals(ELEMENT_NAME_LOCATION)) {
			weather.setCity(attributes.getValue(ATTRIBUTE_NAME_CITY));
			weather.setRegion(attributes.getValue(ATTRIBUTE_NAME_REGION));
			weather.setCountry(attributes.getValue(ATTRIBUTE_NAME_COUNTRY));
		}
		else if (qName.equals(ELEMENT_NAME_ATMOSPHERE)) {
			weather.setHumidity(attributes.getValue(ATTRIBUTE_NAME_HUMIDITY));
			weather.setVisibility(attributes.getValue(ATTRIBUTE_NAME_VISIBILITY));
		}
		else if (qName.equals(ELEMENT_NAME_ASTRONOMY)) {
			weather.setSunrise(attributes.getValue(ATTRIBUTE_NAME_SUNRISE));
			weather.setSunset(attributes.getValue(ATTRIBUTE_NAME_SUNSET));
		}
		else if (qName.equals(ELEMENT_NAME_CONDITION)) {
			weather.setTemperature(attributes.getValue(ATTRIBUTE_NAME_TEMPERATURE));
		}

		xmlStack.push(qName);
	}

	public void endElement (String uri, String localName, String qName) throws SAXException {
		xmlStack.pop();
	}

	public void characters (char ch[], int start, int length) throws SAXException
	{
		if (xmlStack.peek().equals(ELEMENT_NAME_TITLE)) {
			xmlStack.pop();
			if (xmlStack.peek().equals(ELEMENT_NAME_CHANNEL)) {
				weather.setTitle(new String (ch,start,length));
				if (weather.getTitle().equals(ERROR_TITLE))
					throw new InvalidWoeidException("Yahoo! Weather - Error : Invalid WOEID - cannot fetch the RSS");
			}
			xmlStack.push(ELEMENT_NAME_TITLE);
		}
		else if (xmlStack.peek().equals(ELEMENT_NAME_DESCRIPTION)) {
			xmlStack.pop();
			if (xmlStack.peek().equals(ELEMENT_NAME_ITEM)) try {
				javax.xml.parsers.SAXParserFactory saxFactory=javax.xml.parsers.SAXParserFactory.newInstance();
				SAXParser parser=saxFactory.newSAXParser();
				YahooWeatherRSSHandler handler = new YahooWeatherRSSHandler(true);
				String imgDoc = "<html>" + new String(ch,start,length) + "</html>";

				parser.parse(new ByteArrayInputStream(imgDoc.getBytes()) , handler);
				weather.setImageURL(handler.getEmbededImageSrc());
			} catch(Exception e) { e.printStackTrace(); }
			xmlStack.push(ELEMENT_NAME_DESCRIPTION);
			return;
		}
	}   
};

